package com.chen;

import java.util.List;

public class PeopleThread extends Thread{
    // 需要拿到红包集合
    private List<Integer> list;

    public PeopleThread(List<Integer> redPockets, String name) {
        super(name);
        this.list = redPockets;
    }

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        while (true) {
            // 需要判断终止条件 数组为空
            synchronized (list) { // 以共享资源为锁对象
                if (list.isEmpty()) {
                    break;
                }
                // 获取当前线程每次
                int index = (int) (Math.random()*list.size());
                Integer money = list.remove(index);
                System.out.println(name + "抢到红包：" + money);
                if (list.isEmpty()) {
                    System.out.println("红包已经抢光了！");
                    break;
                }
            }
        }
    }
}
